package top.mccat.demo.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.mccat.demo.dao.EmployeeExcellentMapper;
import top.mccat.demo.domain.EmployeeExcellent;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;
import java.util.concurrent.Callable;

/**
 * @author Kevin Li
 * @date 2022/9/7
 * @description
 */
@RestController
@RequestMapping("/download")
public class DownloadController {
    @Autowired
    private EmployeeExcellentMapper employeeExcellentDao;
    @GetMapping("/all")
    public String listAllEmployeeExcellent(HttpServletResponse response) throws IOException {
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("优秀员工","员工"), EmployeeExcellent.class, employeeExcellentDao.findAll());
        OutputStream outputStream = response.getOutputStream();
        response.setCharacterEncoding("UTF-8");
        response.setHeader("content-Type", "application/vnd.ms-excel");
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("优秀员工表.xls", "UTF-8") );
        workbook.write(outputStream);
        workbook.close();
        return "success";
    }

    @GetMapping("/listDate")
    public Date listDate(){
        return new Date();
    }
}
